//package com.uoko.plugins.es.impl;
//
//import org.elasticsearch.action.DocWriteResponse;
//import org.elasticsearch.action.delete.DeleteRequest;
//import org.elasticsearch.action.delete.DeleteResponse;
//import org.elasticsearch.action.get.GetRequest;
//import org.elasticsearch.action.get.GetResponse;
//import org.elasticsearch.action.index.IndexRequest;
//import org.elasticsearch.action.index.IndexResponse;
//import org.elasticsearch.action.search.SearchRequest;
//import org.elasticsearch.action.search.SearchResponse;
//import org.elasticsearch.action.update.UpdateRequest;
//import org.elasticsearch.action.update.UpdateResponse;
//import org.elasticsearch.client.RestHighLevelClient;
//import org.elasticsearch.common.xcontent.XContentType;
//import org.elasticsearch.index.query.QueryBuilder;
//import org.elasticsearch.script.Script;
//import org.elasticsearch.script.ScriptType;
//import org.elasticsearch.search.SearchHits;
//import org.elasticsearch.search.builder.SearchSourceBuilder;
//
//import java.io.IOException;
//
///**
// * @Description DefaultESHttpClient
// * @Author shanjing
// * @Create 2018/5/10-16:55
// * @Version 1.0.0
// */
//public class DefaultESHttpClient extends ESHttpClient {
//
//
//    private final static String MEGACROP = "datas";
//
//    @Override
//    public RestHighLevelClient ESHttpClient(String httpScheme, String httpHost, Integer httpPort) {
//        return super.ESHttpClient(httpScheme, httpHost, httpPort);
//    }
//
//    /**
//     * 创建文档
//     *  IndexResponse[index=tagworks,type=tags,id=111214,version=1,result=created,seqNo=-2,primaryTerm=0,shards={"total":2,"successful":1,"failed":0}]
//     * @param documentId
//     * @param documentJSON
//     * @return
//     */
//    @Override
//    public boolean createDocument(String documentId, String documentJSON) {
//        IndexRequest indexRequest = this.putIndex(MEGACROP,documentId).source(documentJSON , XContentType.JSON);
//        try {
//            IndexResponse indexResponse = this.client.index(indexRequest);
//            if(DocWriteResponse.Result.CREATED == indexResponse.getResult()){
//                return true;
//            }
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//        return false;
//    }
//
//    /**
//     * 删除文档
//     *      DeleteResponse[index=tagworks,type=tags,id=12,version=2,result=deleted,shards=ShardInfo{total=2, successful=1, failures=[]}]
//     * @param documnetId
//     * @return
//     */
//    @Override
//    public boolean deleteDocument(String documnetId) {
//        DeleteRequest deleteRequest = this.delIndex(MEGACROP,documnetId);
//        try {
//            DeleteResponse deleteResponse = this.client.delete(deleteRequest);
//            if(DocWriteResponse.Result.DELETED == deleteResponse.getResult()){
//                return true;
//            }
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//        return false;
//    }
//
//    /**
//     * 通过文档编号查询
//     * @param documnetId
//     * @return
//     */
//    public String getDocumentById(String documnetId){
//        GetRequest getRequest = this.getRequest(MEGACROP,documnetId);
//        try {
//            GetResponse response = this.client.get(getRequest);
//            return response.getSourceAsString();
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//        return null;
//    }
//
//    /**
//     * 修改文档
//     *      {"shardId":{"index":{"name":"tagworks","uuid":"_na_"},"shardId":-1,"hashCode":186962810},"id":"111212","type":"tags","version":2,"seqNo":-2,"primaryTerm":0,"forcedRefresh":false,"result":"NOOP","shardInfo":{"total":0,"successful":0,"failures":[]}}
//     * @param documentId
//     * @param documentJSON
//     * @return
//     */
//    @Override
//    public boolean updateDocument(String documentId, String documentJSON) {
//        UpdateRequest updateRequest = this.pullRequest(MEGACROP,documentId).doc(documentJSON ,XContentType.JSON);
//        try {
//            UpdateResponse response = this.client.update(updateRequest);
//            if(DocWriteResponse.Result.UPDATED == response.getResult())
//                return true;
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//        return false;
//    }
//
//    /**
//     * script update document
//     * eg.
//     *  Map<String, Object> parameters = singletonMap("count", 4);
//     *    Script inline = new Script(ScriptType.INLINE, "painless", "ctx._source.field += params.count", parameters);
//     *    request.script(inline);
//     *  ctx._source Access to the document _source field.
//     *  ctx.op The operation that should be applied to the document: index or delete.
//     *  ctx._index etc Access to document meta-fields, some of which may be read-only.
//     * @param
//     * @return
//     */
//    public void batchUpdateDocument(){
////        UpdateRequest updateRequest = new UpdateRequest();
////
////        Script script = new Script(ScriptType.INLINE,MEGACROP,"ctx._source.remove('strategy')", Maps.newConcurrentMap());
////        updateRequest.script(script);
////        try {
////            UpdateResponse updateResponse =  this.client.update(updateRequest);
////            System.out.println(updateResponse.toString());
////        } catch (IOException e) {
////            e.printStackTrace();
////        }
//
//    }
//
//    @Override
//    public SearchHits searchDocument(QueryBuilder... builders) {
//        SearchRequest searchRequest = this.searchRequest(MEGACROP);
//        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
//        for (QueryBuilder builder : builders) {
//            sourceBuilder.query(builder);
//        }
////        sourceBuilder.from(0); //Set the from option that determines the result index to start searching from. Defaults to 0.
////        sourceBuilder.size(5); //Set the size option that determines the number of search hits to return. Defaults to 10.
////        sourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS)); //Set an optional timeout that controls how long the search is allowed to take.
//        searchRequest.source(sourceBuilder);
//        try {
//            SearchResponse searchResponse = this.client.search(searchRequest);
//            return searchResponse.getHits();
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//        return null;
//    }
//}
